import GameData_blxf from "../GameData/GameData";
import { EnumManager_blxf } from "../Manager/EnumManager";
import { PlatformManager_blxf } from "../Manager/PlatformManager";
import { ConfigModule_blxf } from "./ConfigModule";
import { NumModule_blxf } from "./NumModule";
import { UtilsModule_blxf } from "./UtilsModule";


const {ccclass, property} = cc._decorator;

/** 通用数据 */

@ccclass
export default class PublicModule_blxf {
    /** 是否展示客人文字中 */
    static isDialogueDemo: boolean = false;
    /** 客人播放音效倒计时 */
    static guestAudioTimer: number = 0;

    /** 是否是叠叠乐玩法 */
    static isJengaGame: boolean = false;

    /** 是否加载主玩法界面完成 */
    static isLoadUiOk: boolean = false;

    /** 是否进阶 */
    static isPass: boolean = false;
    /** 金币坐标 */
    static goldIconPos: cc.Vec3 = cc.v3();
    /** 元宝坐标 */
    static diamondIconPos: cc.Vec3 = cc.v3();
    /** 对联坐标 */
    static coupletPos: cc.Vec3 = null;

    static autoGoldIconPos: cc.Vec3 = null;

    static autoDiamondIconPos: cc.Vec3 = null;

    static autoPaperIconPos: cc.Vec3 = null;
    
    /** 是否引导中 */
    static isGui: boolean = false;

    /** 是否重新游戏 */
    static isRestartGame: boolean = false;
    /** 重新游戏前已解锁摊位列表 */
    static restartBfVendorIdList: number[] = [];

    /** 是否游戏中 */
    static isGame: boolean = false;
    /** 玩法页面id（1：主玩法页面、2：自动售卖界面、3：房间内部、4：叠叠乐玩法页面、5：厨房页面） */
    static gamePlayPageId: number = 1;
    /** 玩法页面节点信息 */
    static pageNodeInfo: object = {};

    /** 是否已登录 */
    static isLogin: boolean = false;


    /** 是否卸货 */
    static isRemoveShop: boolean = false;

    /** 是否播放客人语音 */
    static isGuestAudio: boolean = false;
    
    /** 是否摊位升级界面 */
    static isVendorLvUp: boolean = false;

    /** 是否移动中 */
    static isMoveAnimation: boolean = false;

    /** 是否播放补货动画中 */
    static isAddShopAnimation: boolean = false;

    /** 是否过关清除鞭炮 */
    static isPassClearExtra: boolean = false;

    static targetGuestIds: any[] = [];

    /** 自动售卖客人id列表 */
    static autoTargetGuestIds: number[] = [];

    /** 财富榜 */
    static wealthDataList: any[] = [];
    
    /** 游戏玩法父节点 */
    static gamePlayParentNode: cc.Node = null;
    /** 页面切换动画 */
    static pageTween: cc.Tween = null;

    /** 福贴按钮父节点列表（0默认、1暂时） */
    static coupletParentList: cc.Node[] = [];
    /** 是否出现低价购买 */
    static isAllShopTarget: boolean = false;
    /** 记录滑动位置（账本、客源、福贴） */
    static scrollOffsetYs: number[] = [];
    /** 右侧箭头提示倒计时 */
    static rightTipsTime: number = 0;
    /** 左侧箭头提示倒计时 */
    static leftTipsTime: number = 0;

    static dialogueGuestId: number = null;

    /** 剩余增益上货次数 */
    static videoBuffNum: number = 0;
    /** 剩余增益时间 */
    static videoBuffTime: number = 0;

    /** 上次观看视频时间 */
    static videoStartTime: number = -1;
    /** 是否触发负增益 */
    static isTriggerNegative: boolean = false;

    /** 打开的弹窗节点 */
    static openPanelNode: cc.Node = null;

    /** 自动售卖客人路径id列表 */
    static guestPathIdList: number[] = [];
    /** 掉落金币总数 */
    static dropGoldTotalNum: number = 0;

    /** 自动售卖补货 */
    static isAutoSaleNeedAddShop: boolean = false;

    /** 离开主玩法页面累计时间 */
    static leaveMainPlayTime: number = 0;


    /** 玩家进入游戏时间 */
    static gameStartTime: number = -1;

    /** 是否复活成功 */
    static isReviveOk: boolean = false;
    /** 复活可消除数 */
    static reviveRemoveNum: number = 0;
    /** 复活相邻颜色数 */
    static reviveAlikeNum: number  = 0;

    /** 是否叠加中 */
    static isGridMove: boolean = false;
    /** 是否重新检测叠加 */
    static isCheckGrid: boolean = false;
    /** 是否空间不足 */
    static isSpace: boolean = false;

    /** 已打开道具页列表 */
    static openPropPanels: number[] = [];
    /** 是否打开道具使用界面 */
    static isPropUsePanel: boolean = false;

    /** 是否道具使用中 */
    static isPropUse: boolean = false;
    /** 是否道具移除 */
    static isPropRemove: boolean = false;
    /** 是否道具交换 */
    static isPropExchange: boolean = false;
    /** 是否道具刷新 */
    static isPropRefresh: boolean = false;
    /** 标记的道具网格 */
    static markPropGrid: any = null;
    /** 关卡进度基础系数 */
    static passProgressBaseRatio: number = 0;
    /** 关卡进度 */
    static passProgressRatio: number     = 0;

    /** 是否点击住宅按钮 */
    static isTouchRoomBtn: boolean = false;

    /** 已加载分包id列表 */
    static loadBundleIds: number[] = [];
    /** 已加载分包类型列表 */
    static loadBundleTypes: number[] = [];

    /** 资源key */
    static _mapKeys: string[] = [];

    static addBundleIds ( bundleIds: number[] ) {
        let _isCheck: boolean = this.onCheckBundleIds( bundleIds );
        if ( _isCheck ) return;
        bundleIds.forEach( bundleId =>{
            let _isLoad: boolean = this.loadBundleIds.includes( bundleId );
            !_isLoad && this.loadBundleIds.push( bundleId );
        })
    }

    static onCheckBundleIds ( bundleIds: number[] ) {
        let _isCheck: boolean = true;

        bundleIds.forEach( bundleId =>{
            if ( !_isCheck ) return;
            _isCheck = this.loadBundleIds.includes( bundleId );
        })

        return _isCheck;
    }

    static addBundleType ( bundleType: number ) {
        let _isCheck: boolean = this.onCheckBundleType( bundleType );
        !_isCheck && this.loadBundleTypes.push( bundleType );
    }

    static onCheckBundleType ( bundleType: number ) {
        let _isCheck: boolean = this.loadBundleTypes.includes( bundleType );
        return _isCheck;
    }

    static onInitRightTipsTime_blxf () {
        this.rightTipsTime = ConfigModule_blxf.rightTipsCfgTime;
    }

    static onRightTipsTimeTimer_blxf ( dt: number ) {
        if ( this.rightTipsTime <= 0 ) return;
        this.rightTipsTime -= dt;
    }

    static onInitLeftTipsTime_blxf () {
        this.leftTipsTime = ConfigModule_blxf.leftTipsCfgTime;
    }

    static onLeftTipsTimeTimer_blxf ( dt: number ) {
        if ( this.leftTipsTime <= 0 ) return;
        this.leftTipsTime -= dt;
    }

    /** 重置增益数据 */
    static onInitBuffData_blxf () {
        if ( ConfigModule_blxf.gameType == 3 ) return;
        
        let { maxBuffTime, maxBuffNum } = ConfigModule_blxf;
        this.videoBuffNum = maxBuffNum;
        this.videoBuffTime = maxBuffTime;
    }

    /** 更新视频增益时间 */
    static onUpdateBuffTime_blxf ( dt: number ) {
        if ( this.videoBuffTime <= 0 ) return;

        this.videoBuffTime -= dt;
        if ( this.videoBuffTime > 0 ) return;

        this.videoBuffNum = 0;
    }

    /** 更新视频增益次数 */
    static onUpdateBuffNum_blxf () {
        if ( this.videoBuffNum < 0 ) return;

        this.videoBuffNum -= 1;
        if ( this.videoBuffNum > 0 ) return;

        this.videoBuffTime = 0;
    }

    /** 检测是否有视频增益 */
    static onCheckIsVideoBuff_blxf () {
        let _isNum: boolean = this.videoBuffNum > 0;
        let _isTime: boolean = this.videoBuffTime > 0;
        return _isNum || _isTime;
    }

    /** 更新观看视频时间 */
    static onUpdateVideoStartTime_blxf () {
        this.videoStartTime = NumModule_blxf.instance.getTime_blxf();

        this.onUpdateIsTriggerNegative_blxf( false );
    }

    static onUpdateIsTriggerNegative_blxf ( isTrigger: boolean ) {
        this.isTriggerNegative = isTrigger;
    }

    /** 检测触发负增益 */
    static onCheckNegativeBuff_blxf () {
        if ( this.videoStartTime == -1 ) return;
        if ( this.isTriggerNegative ) return;

        let { negativeBigPassNum, negativeCheckNum, negativeCheckTime } = ConfigModule_blxf;

        // 检测大关数
        let _bigPassNum: number = GameData_blxf.instance.getBigPassNum_blxf();
        if ( _bigPassNum < negativeBigPassNum ) return;

        // 检测是否已触发
        let _isTrigger: boolean = GameData_blxf.instance.onCheckNegativeBigPassList_blxf( _bigPassNum );
        if ( _isTrigger ) return;

        // 检测小关数
        let _passNum: number = GameData_blxf.instance.getPassNum_blxf();
        let _smallPassNum: number = ( _passNum - 1 ) % 3 + 1;
        if ( _smallPassNum < negativeCheckNum ) return;

        // 检测上次视频观看时间间隔
        let _nowTime: number = NumModule_blxf.instance.getTime_blxf();
        let _distTime: number = ( _nowTime - this.videoStartTime ) / 1000;
        let _isTime: boolean = _distTime > negativeCheckTime;
        if ( !_isTime ) return;

        this.onUpdateIsTriggerNegative_blxf( true );
        UtilsModule_blxf.instance.Log_blxf('已触发负增益 >>>');

        // 记录当前大关数已触发
        GameData_blxf.instance.setNegativeBigPassList_blxf( _bigPassNum );
    }

    /** 获取是否触发负增益 */
    static getIsTriggerNegative_blxf () {
        return this.isTriggerNegative;
    }

    
    /** 重置离开主玩法累计时间 */
    static onInitLeaveMainPlayTime_blxf () {
        this.leaveMainPlayTime = 0;
    }

    /** 更新离开主玩法累计时间 */
    static onUpdateLeaveMainPlayTime_blxf ( dt: number ) {
        if ( this.gamePlayPageId == 1 && this.isGame ) {
            return this.onInitLeaveMainPlayTime_blxf();
        }

        this.leaveMainPlayTime += dt;
    }

    /** 检测离开主玩法累计时间 */
    static onCheckLeaveMainPlayTime_blxf () {
        let { triggerAngryTime } = ConfigModule_blxf;
        let _isCheck: boolean = this.leaveMainPlayTime > triggerAngryTime;
        return _isCheck;
    }


    /** 更新排行榜数据 */
    static async onWealthDataList_blxf () {
        this.wealthDataList = this.wealthDataList.filter( item =>{
            let { avatar, nickname, num } = item;
            return !!avatar && !!nickname && num < 10 * 10000 * 10000;
        })

        this.wealthDataList = this.wealthDataList.map( (item, index) =>{
            let { rankNum } = item;
            rankNum = rankNum || (index + 1);
            item['rankNum'] = rankNum;

            return item;
        })

        return this.wealthDataList;
    }

    /** 获取玩家排名 */
    static getUserWealthRankNum_blxf ( ) {

    }

    /** 设置打开弹窗状态 */
    static setOpenPanelNode_blxf ( panelNode: cc.Node ) {
        if ( !this.isGame ) return;

        if ( this.openPanelNode && this.openPanelNode.parent ) return;

        if ( this.openPanelNode == panelNode ) return;

        this.openPanelNode = panelNode;

        // 授权按钮显隐
        cc.game.emit('onUpdateBtnAuthorize_blxf', !panelNode);
    }

    /** 新增客人路径id */
    static addGuestPathId_blxf ( pathId: number ) {
        this.guestPathIdList.push( pathId );
        // cc.log('this.guestPathIdList >+++>', pathId, [ ...this.guestPathIdList ]);
    }

    /** 移除客人路径id */
    static removeGuestPathId_blxf ( pathId: number ) {
        let _index: number = this.guestPathIdList.indexOf( pathId );
        this.guestPathIdList.splice( _index, 1 );
        // cc.log('this.guestPathIdList >--->', pathId, [ ...this.guestPathIdList ]);
    }

    /** 获取可去路径id列表 */
    static getGuestPathIdList_blxf ( checkPathIdList: number[] ) {
        let _pathIdList: number[] = [];
        this.guestPathIdList.forEach( pathId =>{
            let _isCheck = _pathIdList.includes( pathId );
            !_isCheck && _pathIdList.push( pathId );
        })

        let _newPathIdList: number[] = checkPathIdList.filter( pathId =>{
            let _isCheck = _pathIdList.includes( pathId );
            return !_isCheck;
        })

        return _newPathIdList;
    }

    /** 累计掉落金币数 */
    static addDropGoldNum_blxf ( num: number ) {
        this.dropGoldTotalNum += num;
    }

    /** 检测是否掉落金币 */
    static onCheckIsDropGold_blxf () {
        let _isMax: boolean = this.dropGoldTotalNum > 1000;
        return _isMax;
    }

    static setIsPass_blxf ( isPass: boolean ) {
        this.isPass = isPass;
    }

    static getIsPass_blxf () {
        return this.isPass;
    }

    static getIsReviveOk_blxf () {
        return this.isReviveOk;
    }

    static setIsReviveOk_blxf ( isOk: boolean ) {
        this.isReviveOk = isOk;
        this.reviveRemoveNum = isOk ? 2 : 0;
        this.reviveAlikeNum  = isOk ? 2 : 0;
    }

    static getReviveRemoveNum_blxf () {
        return this.reviveRemoveNum;
    }

    static getReviveAlikeNum_blxf () {
        return this.reviveAlikeNum;
    }

    /** 更新复活后派发可消除次数 */
    static setReviveRemoveNum_blxf ( num: number ) {
        this.reviveRemoveNum += num;
    }

    /** 更新复活后派发相邻颜色次数 */
    static setReviveAlikeNum_blxf ( num: number ) {
        this.reviveAlikeNum += num;
    }

    /** 设置已打开的道具弹窗 */
    static setOpenPropPanel_blxf ( propId: number ) {
        let _isCheck: boolean = this.onCheckIsOpenProp_blxf( propId );
        !_isCheck && this.openPropPanels.push( propId );
    }

    /** 检测是否已打开道具弹窗 */
    static onCheckIsOpenProp_blxf ( propId: number ) {
        let _isCheck: boolean = this.openPropPanels.includes( propId );
        return _isCheck;
    }

    /** 获取失败所用时间 */
    static onGameFailTime_blxf () {
        let _nowTime: number = NumModule_blxf.instance.getTime_blxf();
        let _distTime: number = _nowTime - this.gameStartTime;
        _distTime = Math.floor( _distTime / 1000 );
        return _distTime;
    }

    /** 累计叠叠乐玩法观看视频次数 */
    static onCheckJengaVideoNum_blxf () {
        let _isJenga: boolean = this.gamePlayPageId == 4;
        if ( !_isJenga ) return;

        GameData_blxf.instance.setJengaVideoNum_blxf();
    }

    /** 检测叠叠乐玩法 */
    static onCheckJenga () {
        this.isJengaGame = ConfigModule_blxf.gameType == 5;
    }
    
}
